﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Net;
using System.Text;

namespace common
{
/// <summary>
/// MakeHtml 的摘要说明
/// </summary>
    public class MakeHtml
    {
        public MakeHtml()
        {
            //
            // TODO: 在此处添加构造函数逻辑
            //
        }
        public static void create(string source, string destiny)
        {
            // Creates an HttpWebRequest with the specified URL. 
            HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(source);
            // Sends the HttpWebRequest and waits for the response.            
            HttpWebResponse myHttpWebResponse = (HttpWebResponse)myHttpWebRequest.GetResponse();
            // Gets the stream associated with the response.
            Stream receiveStream = myHttpWebResponse.GetResponseStream();
            Encoding encode = System.Text.Encoding.UTF8;
            StreamWriter sw = new StreamWriter(destiny, false, encode);
            // Pipes the stream to a higher level stream reader with the required encoding format. 
            StreamReader readStream = new StreamReader(receiveStream, encode);



            Char[] read = new Char[256];
            // Reads 256 characters at a time.    
            int count = readStream.Read(read, 0, 256);
            //Console.WriteLine("HTML...\r\n");
            string str = "";


            while (count > 0)
            {
                // Dumps the 256 characters on a string and displays the string to the console.
                str = new String(read, 0, count);
                sw.Write(str);
                //Console.Write(str);
                count = readStream.Read(read, 0, 256);
            }

            sw.Close();
            readStream.Close();
        }
        public static void createFirstLayer(System.Web.HttpServerUtility Server)
        {
            string sql = "select url,html_url from [column] ";
            DataTable dt1 = TSqlDB.getDataTable(sql);
            string destiny;
            string source;
            ParseXml p1 = new ParseXml(Server.MapPath("~/config/webconfig.xml"));
            string path = p1.parse("path");
            for (int i = 0; i < dt1.Rows.Count; i++)
            {
                destiny = Server.MapPath("~/" + dt1.Rows[i]["url"]).Substring(0, Server.MapPath("~/" + dt1.Rows[i]["url"]).LastIndexOf("\\")) + "\\" + dt1.Rows[i]["html_url"];

                source = path + dt1.Rows[i]["url"];
                if (!destiny.Substring((destiny.Length - 4), 4).Equals("aspx"))
                {
                    MakeHtml.create(source, destiny);

                }

            }
        }


    }
}
